# Descriptive analysis of post sentiments during the 2016 and 2020 US presidential elections.
# See Appendix R.




#############
# load data #
#############

library(ggplot2)
library(ggpubr)

load("PSRM Replication Files/PresidentialElectionsPosts.RData")




######
# us #
######

# subset
us <- president_df[president_df$country == "United States",] 

cor(us[,c("loveprop", "angryprop", "sentiment", "sentiment_norm")],
    use="pairwise.complete.obs")

dl <- us[us$party_country != "Republican Party_United States",
         c("party", "date", "year", "post", "sentiment", "sentiment_norm")]
dl$type <- paste0("(b) ", dl$party)

rl <- us[us$party_country == "Republican Party_United States",
         c("party", "date", "year", "post", "sentiment", "sentiment_norm")]
rl$type <- paste0("(a) ", rl$party)

usgroup <- rbind(dl, rl)
usgroup$type <- factor(usgroup$type, levels=c("(a) Republican Party",
                                              "(b) Democratic Party"))

us2016 <- usgroup[usgroup$year %in% c("2016", "2017"),]
range(us2016$date)

us2020 <- usgroup[usgroup$year %in% c("2020", "2021"),]
range(us2020$date)

# figure R.1 upper panels
p2016 <- ggplot(data = us2016, 
                aes(x = date, y = sentiment_norm, group = post, colour=party)) + 
  geom_vline(xintercept = as.Date("2016-11-08"), lty=2) +
  geom_point(aes(shape = "."), size = 0.9) +
  geom_smooth(method = "loess", lwd=1.5, level=0.95, span=1.2) +
  scale_color_manual(values=c("#0015BC", "#E9141D")) +
  facet_wrap(. ~ type, ncol = 2) +
  ggtitle("2016 Presidential Election") +
  xlab("Date (7/31/2016-2/16/2017)") + 
  ylab("Post Sentiment (Normalized)") +
  coord_cartesian(ylim=c(-0.3, 0.5)) +
  theme_bw() +
  theme(legend.position = "none",
        axis.title.x = element_text(size = 15),
        axis.title.y = element_text(size = 15))

# figure R.1 lower panels
p2020 <- ggplot(data = us2020,
                aes(x = date, y = sentiment_norm, group = post, colour=party)) + 
  geom_vline(xintercept = as.Date("2020-11-03"), lty=2) +
  geom_point(aes(shape = "."), size = 0.9) +
  geom_smooth(method = "loess", lwd=1.5, level=0.95, span=1.2) +
  scale_color_manual(values=c("#0015BC", "#E9141D")) +
  facet_wrap(. ~ type, ncol = 2) +
  ggtitle("2020 Presidential Election") +
  xlab("Date (7/26/2020-2/11/2021)") + 
  ylab("Post Sentiment (Normalized)") +
  coord_cartesian(ylim=c(-0.3, 0.5)) +
  theme_bw() +
  theme(legend.position = "none",
        axis.title.x = element_text(size = 15),
        axis.title.y = element_text(size = 15))

ggarrange(p2016, p2020,
          ncol = 1, nrow = 2)

#ggsave("ussentiment.pdf", width=12, height=10)
